package com.windowes.weichang.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.windowes.weichang.access.UserContext;
import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.SysRoleDeptMapper;
import com.windowes.weichang.dao.SysUserRoleMapper;
import com.windowes.weichang.dao.WUserMapper;
import com.windowes.weichang.dto.StaffDTO;
import com.windowes.weichang.pojo.SysUserRoleKey;
import com.windowes.weichang.pojo.WUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/7/28 下午7:29
 * @description:
 *
 **/
@Service
public class StaffService {


    @Autowired
    WUserMapper userMapper;
    @Autowired
    SysRoleDeptMapper sysRoleDeptMapper;
    @Autowired
    SysUserRoleMapper userRoleMapper;


    /**
     * 获取员工列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public ServerResponse listStaff(int pageNo, int pageSize) {
        Integer deptId = UserContext.getDeptId();
        Page<WUser> userPage = new Page(pageNo, pageSize);
        userPage.setRecords(userMapper.selectbyDeptId(userPage, deptId));
        return ServerResponse.createBySuccess(userPage);
    }

    /**
     * 添加员工
     *
     * @param staff
     * @return
     */
    @Transactional
    public ServerResponse addOrUpdate(StaffDTO staff) {
        if (StringUtils.isEmpty(staff.getPhone())) {
            return ServerResponse.createByErrorMessage("请输入手机号!");
        }

        if (staff.getRoleId() == null) {
            return ServerResponse.createByErrorMessage("请设置角色!");
        }


        WUser wUser = userMapper.selectByPhone(staff.getPhone());

        if (staff.getId() == null) {
            //新增用户
            if (wUser != null) {
                return ServerResponse.createByErrorMessage("手机号已存在,请换个手机号试试!");
            }

            Integer deptId = UserContext.getDeptId();
            staff.setDeptId(deptId);
            staff.setPassword("123456");
            userMapper.insertSelective(staff);
            //新增角色

            SysUserRoleKey ur = new SysUserRoleKey();
            ur.setRoleId(staff.getRoleId());
            ur.setUserId(staff.getId());
            userRoleMapper.insertSelective(ur);
        } else {
            if (staff.getLastRoleId() == null) {
                return ServerResponse.createByErrorMessage("请设置lastRoleId!");
            }
            //校验手机号
            if (!wUser.getId().equals(staff.getId())) {
                return ServerResponse.createByLoginErrorMessage("手机号已被占用！");
            }
            //更新用户信息
            userMapper.updateByPrimaryKeySelective(staff);
            //删除角色关系
            SysUserRoleKey userRoleKeyQ = new SysUserRoleKey();
            userRoleKeyQ.setUserId(wUser.getId());
            userRoleKeyQ.setRoleId(staff.getLastRoleId());
            SysUserRoleKey userRoleKey = userRoleMapper.selectByKey(userRoleKeyQ);
            if (userRoleKey != null) {
                //更新角色
                userRoleMapper.deleteByPrimaryKey(userRoleKey);
                SysUserRoleKey ur = new SysUserRoleKey();
                ur.setRoleId(staff.getRoleId());
                ur.setUserId(staff.getId());
                userRoleMapper.insertSelective(ur);
            } else {
                //插入角色
                SysUserRoleKey ur = new SysUserRoleKey();
                ur.setRoleId(staff.getRoleId());
                ur.setUserId(staff.getId());
                userRoleMapper.insertSelective(ur);
            }

        }
        return ServerResponse.createBySuccessMessage("操作成功!");
    }

    public ServerResponse limit(Integer userId, Boolean limit) {
        if (limit == null) {
            return ServerResponse.createByErrorMessage("请设置状态!");
        }

        if (userId == null) {
            return ServerResponse.createByErrorMessage("请选择用户!");
        }

        WUser wUser = userMapper.selectByPrimaryKey(userId);
        if (wUser == null) {
            return ServerResponse.createByErrorMessage("该用户已被删除,请核实!");
        }

        WUser user = new WUser();
        user.setId(userId);
        user.setLimitedLogin(limit);
        userMapper.updateByPrimaryKeySelective(user);
        return ServerResponse.createBySuccessMessage("操作成功!");
    }
}
